$(function(){
	getShopCarList();
})

//获取购物车列表
function getShopCarList(){
	var arr={
		hf_s_id:"WD11",
		hf_srv_id:"Av_jcart",
	};
	getAjaxRequest(arr,function(result){
		$('.total em').html(result.bj_total_item_count);//设置购物车中商品总数
		$('.total span').html("$"+result.bj_total_item_amount);//获取购物车中商品总价格
		$('.sumPrice span').html("$"+result.bj_total_item_amount);//获取购物车中商品总价格

		var BvcCcartIitem=result.bvc_cart_item;
		var carList='';
		if(BvcCcartIitem.length != 0){
			for (var i=0 ;i<BvcCcartIitem.length;i++){

				carList+='<div class="item" data-id="'+BvcCcartIitem[i].bj_pdt_id+'" data-item-id="'+BvcCcartIitem[i].bs_item_no+'">'+
					'<div class="mainCon">'+
						'<div class="proImg">'+
							'<img src="'+MPJ.apiBase+''+BvcCcartIitem[i].bs_simg_file+'"/>'+
							''+(BvcCcartIitem[i].bs_prom_reminder_ind == "Y" ? '<span><em>offer</em></span>' : '' )+
					'</div>'+
					'<div class="mui-media-body">'+
						'<div class="pro-name">'+BvcCcartIitem[i].bs_brand+'</div>'+
						'<div class="pro-f-name">'+BvcCcartIitem[i].bs_desc+'</div>'+
						'<div class="price">$<em>'+BvcCcartIitem[i].bj_price_inuse+'</em></div>'+
						''+(BvcCcartIitem[i].bs_prom_reminder_ind == "N" ? '<div class="yh">'+BvcCcartIitem[i].bs_prom_text+'</div>' : '' )+
						'<div class="price-num">'+
							'<span>Qty:</span>'+
								'<div class="mui-numbox" data-numbox-min="1" >'+
									'<button class="mui-btn mui-numbox-btn-minus" type="button">-</button>'+
									'<input class="mui-numbox-input" type="number" value="'+BvcCcartIitem[i].bj_qty+'" />'+
									'<button class="mui-btn mui-numbox-btn-plus" type="button">+</button>'+
								'</div>'+
							'</div>'+
						'</div>'+
						'<div class="deleteBtn">'+
							'<img src="'+MPJ.base+'/resources/images/icon/delete-icon.png" />'+
						'</div>'+
					'</div>'+
					''+(BvcCcartIitem[i].bs_prom_reminder_ind == "Y" ? '<div class="discount">'+
					'<p>Not to miss! (Please click to enjoy offer) </p>'+
					''+BvcCcartIitem[i].bs_prom_text+''+
					'</div>' : '' )+
				'</div>';
			}
		}else{
			carList+='<div class="nodate">Your shopping cart is empty!</div>';
		}
		$('.allPro.horizontal-list').html(carList);
		mui('.mui-numbox').numbox();


		var bvc_cart_prom_miss_g3=result.bvc_cart_prom_miss_g3;
		var bvc_prom_str=''
		if(bvc_cart_prom_miss_g3.length != 0){
			for (var i=0 ;i<bvc_cart_prom_miss_g3.length;i++){
				bvc_prom_str+='<li data-prom-no="'+bvc_cart_prom_miss_g3[i].bs_promo_no+'">'+bvc_cart_prom_miss_g3[i].bs_prom_text+'</li>';
			}
			$('.see-offer ul').html(bvc_prom_str);
		}

		OperShoppingCart();//操作购物车函数
	});
}
//操作购物车函数函数
function  OperShoppingCart(){
	$('.allPro .item').each(function(index){
		var $this=$(this);
		var id=$(this).data("id");//获取商品bj_pdt_id
		var item_id=$(this).data("item_id");//获取bs_item_no
		//跳转到商品详情页
		$this.on('tap','.proImg',function(){
			var pdt_id=$this.data('id');
			var item_no=$this.data('item-no');
			var product_info={};
			product_info.pdt_id=pdt_id;
			product_info.item_no=item_no;
			sessionStorage.setItem('product_info',JSON.stringify(product_info));
			location.href=MPJ.base+"/pages/product/product-detail.jsp";
		})
		$this.on('tap','.addCar',function(){
			location.href=MPJ.base+"/pages/cart/list.jsp";

		})

		//更新里面的商品数量
		var $input=$(this).find('input.mui-numbox-input');
		$input.on('change',function(){
			var arr={
				hf_s_id:"WD11",
				hf_srv_id:"Av_jcart",
				hs_action_id:"update_to_cart",
				hs_buy_pdt_id:id,
				hvs_cart_pdt_id:id,
				hvs_cart_qty:$input.val()
			};
			getAjaxRequest(arr,function(result){
				if (result.bs_result=BS_RESULT.OK){
					getShopCarList();
				}
			});
		})

		//删除购物车
		var $btn=$this.find('.deleteBtn')
		$btn.on('tap',function(){
			var arr={
				hf_s_id:"WD11",
				hf_srv_id:"Av_jcart",
				hs_action_id:"delete_from_cart",
				hs_buy_pdt_id:id,
				hvs_cart_pdt_id:id,
				hs_del_pdt_pos:0,
			};
			getAjaxRequest(arr,function(result){
				if (result.bs_result=BS_RESULT.OK){
					dialog('Message', 'Remove Goods Successfully ', 1);
					var $dialog = $('#dialog');
					$dialog.show();
					$dialog.find('.comfirmBtn').one('click', function() {
						$dialog.hide();
						$dialog.remove();
						getShopCarList();
					});

				}
			});
		})

		//跳转到优惠页面
		var $discount=$this.find('.discount');
		$discount.on('tap',function(){
			location.href=MPJ.base+"/pages/cart/shopcar-offer.jsp";
		})


		//优惠页面
		$('.see-offer ul li').on('tap',function(){
			var index=$(this).index();
			if(index%2==0){
				location.href=MPJ.base+"/pages/cart/shopcar-offer1.jsp";
			}else{
				location.href=MPJ.base+"/pages/cart/shopcar-offer2.jsp";
			}
		})


	})
}


//清空购物车
$('.empty-check .empty').on('tap',function(){
	var arr={
		hf_s_id:"WD11",
		hf_srv_id:"Av_jcart",
		hs_action_id:"clr_cart"
	};
	getAjaxRequest(arr,function(result){
		if (result.bs_result=BS_RESULT.OK){
			dialog('Message', 'Empty cart success', 1);
			var $dialog = $('#dialog');
			$dialog.show();
			$dialog.find('.comfirmBtn').one('click', function() {
				$dialog.hide();
				$dialog.remove();
				mui('#offCanvasContentScroll').scroll().scrollTo(0,0,100);
				getShopCarList();
			});

		}
	});
})






$('.expressCheckOut').on('tap',function(){
	$('.allPro .item').remove();
	getSumPrice();
})

//数量减少
$('.mui-numbox-input').each(function(index){
	$(this).change(function(){
		getSumPrice();
	})
})


$('.offer-list .saveTo').on('tap',function(){
	location.href=MPJ.base+"/pages/account/shopping/add.jsp";
})


$('.changeBox .changeBtn').on('tap',function(){
	location.href=MPJ.base+"/pages/change-delivery-location.jsp";
})
$('.changeBox span').on('tap',function(){
	location.href=MPJ.base+"/pages/select-delivery-location.jsp";
})

//优惠列表的显示与隐藏
$('.see-offer h4').on('tap',function(){
	var $listBox = $('.see-offer ul');
    var $img = $('.see-offer  h1 span img');
    if ($listBox.css('display') == "none") {
        $listBox.slideDown();
        $img.attr('src', MPJ.base+'/resources/images/icon/delivery-jt-up.png');
    } else {
        $listBox.slideUp();
        $img.attr('src', MPJ.base+'/resources/images/icon/delivery-jt-down.png');
    }
})


//回到顶部
$('.goTop').on("tap",function(){
	mui('#offCanvasContentScroll').scroll().scrollTo(0,0,100);
})

//跳转到确认订单
$('.empty-check .checkOut').on("tap",function(){
	var type=sessionStorage.getItem('type');
	if(type==1){
		location.href=MPJ.base+"/pages/order/flower-confirm-detail.jsp";
	}else{
		location.href=MPJ.base+"/pages/order/confirm-detail.jsp";
	}
})

